Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var record_start = 0; 

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40,
	renderer:function(value,metadata,record,rowIndex){ 
		return record_start + 1 + rowIndex; 
	} 
}),

{header:'保证人编号',dataIndex:'guarantorId',width:100,sortable:true},
{header:'保证人类型',dataIndex:'guarantorType',width:100,sortable:true},
{header:'保证人客户号',dataIndex:'custNo',width:100,sortable:true},
{header:'保证人名称',dataIndex:'custName',width:100,sortable:true},
{header:'是否为本行客户',dataIndex:'isOurCustomer',width:100,sortable:true},
]); 

var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'obligation/listGuarantor.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({
	totalProperty:'total',  
	root:'guarantory'
},[
{name:'guarantorType'},
{name:'custNo'}, 
{name:'custName'},  
{name:'isOurCustomer'},
{name:'guarantorId'},
]) 
}); 
ds.load({params:{start:0,limit:20}}); 


var addGuarantyTypeLevelOneStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showGuarantyTypeLevelOne.do',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addGuarantyTypeLevelTwoStore=new Ext.data.Store({  
		reader:new Ext.data.JsonReader({ },[
		{name:'name'}, 
		{name:'code'}
	])
}); 

var addGuarantyStyleStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=GuarantyStyle',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addGuaranteeStyleStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=GuaranteeStyle',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addGuarantorStyleStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=GuarantorStyle',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addGuarantorTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=GuarantorType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addIsOurCustStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=YesNo',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
}); 

var addCustTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) ,
autoLoad:true
}); 

//var addGuarantyFormStore=new Ext.data.Store({ 
//	proxy:new Ext.data.HttpProxy({
//	url:'showType.do?type=GuarantyForm',
//	method:'GET'
//	}), 
//	reader:new Ext.data.JsonReader({ },[
//	{name:'name'}, 
//	{name:'code'}
//	]) ,
//	autoLoad:true
//}); 
//
//var addGuaranteeFormStore=new Ext.data.Store({ 
//	proxy:new Ext.data.HttpProxy({
//	url:'showType.do?type=GuaranteeForm',
//	method:'GET'
//	}), 
//	reader:new Ext.data.JsonReader({ },[
//	{name:'name'}, 
//	{name:'code'}
//	]) ,
//	autoLoad:true
//}); 

var addGuarantyRelativeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=GuarantyRelativeType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addCustScopeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustScope',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addCustListStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showCustList.do',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addExtRatOrgStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=ExtRatOrg',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]),
autoLoad:true
}); 

var conditionStore = new Ext.data.SimpleStore({
	   fields : ['id', 'text'],
	   data : [['guarantorId', '保证人编号'], ['custNo', '保证人客户号'], ['custName', '保证人名称']]
});

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"保证人列表", 
height:500, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据",
                doLoad : function(start){ 
             	   record_start = start; 
             	var o = {}, pn = this.getParams(); 
             	          o[pn.start] = start; 
             	          o[pn.limit] = this.pageSize; 
             	this.store.load({params:o}); 
              }
            }),
 tbar: [
	 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:checkDetail},
 '-',
 {text:'删除',handler:del},
 '->',
	{xtype:'combo',
  id:'condition',
  name: 'condition', 
  store: conditionStore,
  fieldLabel: '请选择查询条件',
  valueField : 'id',
  displayField : 'text',
  emptyText : '请选择查询条件',
  triggerAction : 'all',
  editable: false,
  mode:'local',
  width:110
  },'',
  {xtype:'textfield',id:'keyword',width:180},
  {text:'查询',handler:findCustomer},
  '-',
  {text:'清空',handler: callBack}  
 ]              
});


function findCustomer(){

var condition = Ext.getCmp('condition').getValue();
var keyword = Ext.getCmp('keyword').getValue();

if(condition == '' || condition == null){
	Ext.Msg.alert('提示', '请选择查询条件！'); 
}
else{

	ds.baseParams={condition:condition, keyword: keyword};
	ds.proxy = new Ext.data.HttpProxy({
					url:'obligation/findGuarantor.do',
					method:'POST'
				}); 
	ds.reader = new Ext.data.JsonReader({ 
						totalProperty:'total',  
						root:'resultData'},[
						{name:'guarantorType', mapping: 'guarantorType'}, 
						{name:'custNo', mapping: 'custNo'},  
						{name:'custName', mapping: 'custName'},
						{name:'guarantorId', mapping: 'guarantorId'},
						{name:'isOurCustomer', mapping: 'isOurCustomer'},
					]);
					   
	ds.load({params:{start:0,limit:20}});
	record_start = 0;  	
}
}

function callBack(){

Ext.getCmp('condition').clearValue();
Ext.getCmp('keyword').setValue('');
	ds.proxy = new Ext.data.HttpProxy({
					url:'obligation/findGuarantor.do',
					method:'POST'
				}); 
	ds.reader = new Ext.data.JsonReader({ 
						totalProperty:'total',  
						root:'resultData'},[
						                    {name:'guarantorType', mapping: 'guarantorType'}, 
											{name:'custNo', mapping: 'custNo'},  
											{name:'custName', mapping: 'custName'},
											{name:'guarantorId', mapping: 'guarantorId'},
											{name:'isOurCustomer', mapping: 'isOurCustomer'},
					]);   
	ds.load({params:{start:0,limit:20}});
	record_start = 0;

}


//新增客户表单
addGuarantorForm = new Ext.form.FormPanel({ 
id: 'addGuarantorForm', 
width: 370, 
height: 500,
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset', 
title: '客户信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ 
{xtype: 'combo', 
id : "guarantorType",
store :addGuarantorTypeStore,
fieldLabel: '保证人类型',
valueField : 'code',
displayField : 'name',
typeAhead : true,
width: 200,
mode:'remote', 
emptyText : '请选择保证人类型...',
allowBlank: false,  
blankText: '保证人类型不能为空', 
editable: false,
triggerAction : 'all',
selectOnFocus : true,
hiddenName:'guarantorType',
listeners:{  	        	   
	   select : function(combo, record,index){
		   		//alert(combo.value);
		   		if(combo.value == "01"){
		   			Ext.getCmp('OrganizationID').hide();
		   			Ext.getCmp('BusiLicenseID').hide();
		   			Ext.getCmp('OrganizationID').hide();
		   			Ext.getCmp('custType').hide();
		   			Ext.getCmp('custScope').hide();
		   			Ext.getCmp('creditLevel').hide();
		   			Ext.getCmp('creditDate').hide();
		   			Ext.getCmp('CustName').show();
		   			Ext.getCmp('CustNo').show();
		   			Ext.getCmp('isOurCustomer').show();
		   			Ext.getCmp('identityCardId').show();
		   			Ext.getCmp('personalAsset').show();
		   			Ext.getCmp('personalDebt').show();
		   			Ext.getCmp('annualIncome').show();	
		   		}else{
		   			Ext.getCmp('OrganizationID').show();
		   			Ext.getCmp('BusiLicenseID').show();
		   			Ext.getCmp('OrganizationID').show();
		   			Ext.getCmp('custType').show();
		   			Ext.getCmp('custScope').show();
		   			Ext.getCmp('creditLevel').show();
		   			Ext.getCmp('creditDate').show();
		   			Ext.getCmp('CustName').show();
		   			Ext.getCmp('CustNo').show();
		   			Ext.getCmp('isOurCustomer').show();
		   			Ext.getCmp('identityCardId').hide();
		   			Ext.getCmp('personalAsset').hide();
		   			Ext.getCmp('personalDebt').hide();
		   			Ext.getCmp('annualIncome').hide();
		   		}
		   }
	   }



}
,
{ 
	xtype: 'textfield', 
	id:'CustNo',
	name: 'CustNo', 
	fieldLabel: '客户号', 
//	emptyText: '必填', 
//	blankText: '角色编号不能为空', 
//	allowBlank: false, 
	width:200,
	maxLength: 40, 
	maxLengthText: '角色不能超过40个字符', 
	hidden:true,
	},
{ 
xtype: 'textfield', 
id:'CustName',
name: 'CustName', 
fieldLabel: '客户名称', 
emptyText: '必填', 
blankText: '角色名称不能为空', 
allowBlank: false, 
maxLength: 50, 
width:200, 
maxLengthText: '角色不能超过50个字符', 
hidden:true,	
}, 

{ 
xtype: 'textfield',
id: 'OrganizationID', 
name: 'OrganizationId', 
fieldLabel: '组织机构代码', 
width:200,  
hidden:true
},

{ 
xtype: 'textfield', 
id: 'BusiLicenseID',
name: 'BusiLicenseId', 
fieldLabel: '营业执照代码', 
width:200,
hidden:true,
},

{ 
	xtype: 'textfield', 
	id: 'identityCardId',
	name: 'identityCardId', 
	fieldLabel: '身份证号', 
	width:200,
	hidden:true,
},

{      
	xtype: 'textfield', 
			id: 'personalAsset',
			name: 'personalAsset', 
			fieldLabel: '个人资产总额（元）', 
			allowBlank: true,  
			width: 200,
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
				regexText:'只能输入整数或小数！' ,
			hidden:true,
},

{      
	xtype: 'textfield', 
			id: 'personalDebt',
			name: 'personalDebt', 
			fieldLabel: '个人负债总额（元）', 
			allowBlank: true,  
			width: 200,
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
			regexText:'只能输入整数或小数！' ,
			hidden:true,
},

{      
	xtype: 'textfield', 
			id: 'annualIncome',
			name: 'annualIncome', 
			fieldLabel: '年收入（元）', 
			allowBlank: true,  
			width: 200,
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
			regexText:'只能输入整数或小数！' ,
			hidden:true,
},


{ 
xtype: 'combo', 
	id : "custType",
            store :addCustTypeStore,
            fieldLabel: '客户种类',
            valueField : 'code',
            displayField : 'name',
            typeAhead : true,
            mode:'remote', 
            emptyText : '请选择客户种类...',
            editable: false, 
			  width:200,
            triggerAction : 'all',
            selectOnFocus : true,
            hiddenName:'custType',
            hidden:true,
},
{ 
xtype: 'combo', 
	id : "custScope",
            store :addCustScopeStore,
            fieldLabel: '保证人规模',
            valueField : 'code',
            displayField : 'name',
            typeAhead : true,
            mode:'remote', 
            emptyText : '请选择保证人规模...',
            editable: false, 
			  width:200,
            triggerAction : 'all',
            selectOnFocus : true,
            hiddenName:'custScope',
            hidden:true,
},

{ layout : "form",
         items : [{
			xtype: 'textfield',
			id: 'creditLevel', 
			name: 'creditLevel', 
			fieldLabel: '最后一次信用评级',  
			allowBlank: true,  
			width:200, 
			hidden:true,
}]},

{ 
layout : "form",
	items : [{
			xtype: 'datefield',
			id: 'creditDate', 
			name: 'creditDate', 
			fieldLabel: '最后一次信用评级时间',  
			allowBlank: true, 
			width:200,
			format : 'Y-m-d',
			hidden:true,
}]} ,

{ 
	layout : "form",
		items : [{
			 xtype: 'combo', 
		     id : "isOurCustomer",
		  store :addIsOurCustStore,
        fieldLabel: '是否为我行客户',
        valueField : 'code',
        displayField : 'name',
        typeAhead : true,
        width : 200	,
        mode:'remote', 
        emptyText : '请选择是否为我行客户...',
        allowBlank: false,  
        blankText: '是否为我行客户不能为空', 
        editable: false,
        triggerAction : 'all',
        selectOnFocus : true,
        hiddenName:'isOurCustomer',   
        hidden:true,
}]} ,
{xtype:'hidden',
	id:'guarantorId',
	name:'guarantorId'
	}
] 
}]
});

//新增用户窗口 
addGuarantorWin = new Ext.Window({ 
id: 'addGuarantorWin', 
title: '新增保证人', 
width: 380, 
height: 400, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addGuarantorForm, 
buttons: 
[  
{ text: '保存', id: 'btnGuarantorSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addGuarantorWin').hide(); } }

] 
});

function addFunction() {

	if(addGuarantorForm.form.isValid()){
		addGuarantorForm.form.doAction('submit', { 
			url : 'obligation/addGuarantor.do', 
			method : 'post', 
			success : function() { 
			Ext.getCmp('addGuarantorWin').hide();
			grid.store.reload();
			Ext.Msg.alert('提示', '保存成功！'); 
			}, 
			failure : function() { 
			Ext.Msg.alert('提示', '保存失败！'); 
			} 
		});  
	}
}

//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp('addGuarantorForm').form.reset(); 
Ext.getCmp("addGuarantorWin").buttons[0].handler = addFunction; 
Ext.getCmp("addGuarantorWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除客户
function del(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
	Ext.Msg.alert("提示", "请选中要删除的内容"); 
} 
else{
	Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
 Ext.Ajax.request({
  url:'obligation/deleteGuarantor.do',
  params : {guarantorId : grid.getSelectionModel().getSelections()[0].data.guarantorId},
  method:'POST',
  success:function(response){
	  var json = Ext.util.JSON.decode(response.responseText);
	  if(json.del == true){
		  Ext.MessageBox.alert("提示","删除成功！");
	   	grid.store.reload(); 
	  }
	  else if(json.del == false){
		  Ext.MessageBox.alert("提示","该保证人与债项有关联，不能删除！");
	  }
	   
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

function editFunction(){
	
if(addGuarantorForm.form.isValid()){
	 addGuarantorForm.form.doAction('submit', { 
        url : 'obligation/editGuarantor.do', 
        method : 'post', 
		success : function() { 
			grid.store.reload();
			Ext.Msg.alert('提示', '保存成功！'); 
		}, 
        failure : function() { 
			Ext.Msg.alert('提示', '保存失败！'); 
 		} 
	}); 
}
}

function checkDetail(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要查看的内容！"); 
}
else{
	var selectedId = grid.getSelectionModel().getSelections()[0].data.guarantorId;
           
   	addGuarantorForm.form.reset(); 
	
	addGuarantorWin.buttons[0].handler = editFunction; 

	Ext.Ajax.request({
		url:'obligation/getGuarantor.do',
		params : { guarantorId: selectedId},
		method:'POST',
  		success:function(response){
		 	var json = Ext.util.JSON.decode(response.responseText);
		 	Ext.getCmp("guarantorId").setValue(json.guarantorId);
		 	Ext.getCmp("guarantorType").setValue(json.guarantorType);
		 	Ext.getCmp("OrganizationID").setValue(json.organizationId);
		  	Ext.getCmp("BusiLicenseID").setValue(json.busiLicenseId);
			Ext.getCmp("custType").setValue(json.custType);
			Ext.getCmp("custScope").setValue(json.custScope);
			Ext.getCmp("creditLevel").setValue(json.creditLevel);
		  	Ext.getCmp("creditDate").setValue(json.creditDate);
			Ext.getCmp("CustName").setValue(json.custName);
		   	Ext.getCmp("CustNo").setValue(json.custNo);
			Ext.getCmp("isOurCustomer").setValue(json.isOurCustomer);
			Ext.getCmp("identityCardId").setValue(json.identityCardId);
		  	Ext.getCmp("personalAsset").setValue(json.personalAsset);
		  	Ext.getCmp("personalDebt").setValue(json.personalDebt);
		  	Ext.getCmp("annualIncome").setValue(json.annualIncome);
		 	
		 	if(json.guarantorType=="01"){
		 		Ext.getCmp('OrganizationID').hide();
	   			Ext.getCmp('BusiLicenseID').hide();
	   			Ext.getCmp('custType').hide();
	   			Ext.getCmp('custScope').hide();
	   			Ext.getCmp('creditLevel').hide();
	   			Ext.getCmp('creditDate').hide();
	   			Ext.getCmp('CustName').show();
	   			Ext.getCmp('CustNo').show();
	   			Ext.getCmp('isOurCustomer').show();
	   			Ext.getCmp('identityCardId').show();
	   			Ext.getCmp('personalAsset').show();
	   			Ext.getCmp('personalDebt').show();
	   			Ext.getCmp('annualIncome').show();	
		 	}else{
		 		Ext.getCmp('OrganizationID').show();
	   			Ext.getCmp('BusiLicenseID').show();
	   			Ext.getCmp('custType').show();
	   			Ext.getCmp('custScope').show();
	   			Ext.getCmp('creditLevel').show();
	   			Ext.getCmp('creditDate').show();
	   			Ext.getCmp('CustName').show();
	   			Ext.getCmp('CustNo').show();
	   			Ext.getCmp('isOurCustomer').show();
	   			Ext.getCmp('identityCardId').hide();
	   			Ext.getCmp('personalAsset').hide();
	   			Ext.getCmp('personalDebt').hide();
	   			Ext.getCmp('annualIncome').hide();
		 	}
		 	
			Ext.getCmp("addGuarantorWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}

grid.addListener('rowdblclick',checkDetail);
}); 